package com.aruba.screenlibrary;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 逐层处理viewgroup
 */
public class ViewsParseUtil {

    /**
     * 对activity中的所有控件进行适配
     *
     * @param activity
     */
    protected static void parseAcitvity(Activity activity) {
        //获取DecorView
        final ViewGroup context = (ViewGroup) activity.findViewById(android.R.id.content);
        context.post(new Runnable() {
            @Override
            public void run() {
                catchViewGroup(context);
            }
        });
    }

    /**
     * 对组件进行适配
     *
     * @param viewGroup
     */
    public static void catchViewGroup(ViewGroup viewGroup) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View view = viewGroup.getChildAt(i);
            if (view instanceof ViewGroup) {
                catchViewGroup((ViewGroup) view);
            } else {
                catchChildView(view);
            }
        }
    }

    /**
     * 对控件进行适配
     *
     * @param view
     */
    public static void catchChildView(View view) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();

        //设置LayoutParams
        ViewCalculateUtil.setLayoutParams(view, layoutParams.width, layoutParams.height, layoutParams.topMargin, layoutParams.bottomMargin,
                layoutParams.leftMargin, layoutParams.rightMargin);
        if (view instanceof TextView) {//如果是textview，设配字体大小
            TextView textView = (TextView) view;
            ViewCalculateUtil.setTextSize(textView, (int) textView.getTextSize());
        }
        //设置padding
        ViewCalculateUtil.setViewPadding(view, view.getPaddingTop(), view.getPaddingBottom(), view.getPaddingLeft(), view.getPaddingRight());
    }

}
